package flash;

import gnu.io.SerialPortEvent;
import gnu.io.SerialPortEventListener;

import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.swt.graphics.Point;
import org.eclipse.ui.application.ActionBarAdvisor;
import org.eclipse.ui.application.IActionBarConfigurer;
import org.eclipse.ui.application.IWorkbenchWindowConfigurer;
import org.eclipse.ui.application.WorkbenchWindowAdvisor;

import com.akeil.flash.preferences.IPreferenceConstants;
import com.akeil.flash.statusline.StatisBarData;
import com.yang.serialport.exception.NoSuchPort;
import com.yang.serialport.exception.NotASerialPort;
import com.yang.serialport.exception.PortInUse;
import com.yang.serialport.exception.SerialPortParameterFailure;
import com.yang.serialport.exception.TooManyListeners;
import com.yang.serialport.manage.SerialPortManager;

public class ApplicationWorkbenchWindowAdvisor extends WorkbenchWindowAdvisor {
	private IPreferenceStore pre = Activator.getDefault().getPreferenceStore();
	
	public ApplicationWorkbenchWindowAdvisor(IWorkbenchWindowConfigurer configurer) {
		super(configurer);
	}

	public ActionBarAdvisor createActionBarAdvisor(
			IActionBarConfigurer configurer) {
		return new ApplicationActionBarAdvisor(configurer);
	}

	public void preWindowOpen() {
		IWorkbenchWindowConfigurer configurer = getWindowConfigurer();
		configurer.setInitialSize(new Point(800, 600));
		configurer.setShowCoolBar(true);
		configurer.setShowStatusLine(true);
		configurer.setTitle("闪点控制软件");
	}
	
	@Override
	public void postWindowOpen() {
		// TODO 自动生成的方法存根
		super.postWindowOpen();
		openSerialPort(null);
		new StatisBarData();
		Application.statusTimerManager.startTimerTask();
		Application.serialTimerManager.startTimerTask();
	}
	
	@Override
	public boolean preWindowShellClose() {
		// TODO 自动生成的方法存根
		closeSerialPort();
		
		return super.preWindowShellClose();
	}

	private void openSerialPort(java.awt.event.ActionEvent evt) {
		String commName = pre.getString(IPreferenceConstants.SERIAL_PORT);
		int baudrate = Integer.parseInt(pre.getString(IPreferenceConstants.SERIAL_BAUD));
		String databits = pre.getString(IPreferenceConstants.SERIAL_DATABITS);
		String stopbits = pre.getString(IPreferenceConstants.SERIAL_STOPBITS);
		String parity = pre.getString(IPreferenceConstants.SERIAL_PARITY);
		
		try {
			Application.serialport = SerialPortManager.openPort(commName, baudrate,databits,stopbits,parity);
			
			if (Application.serialport != null) {
				
			}
		} catch (SerialPortParameterFailure e) {
			e.printStackTrace();
		} catch (NotASerialPort e) {
			e.printStackTrace();
		} catch (NoSuchPort e) {
			//ShowUtils.errorMessage(e.toString());
		} catch (PortInUse e) {
			e.printStackTrace();
			//ShowUtils.warningMessage("COM口已占用！");
		}
		
		if(Application.serialport != null){
			try {
				SerialPortManager.addListener(Application.serialport, new SerialListener());
			} catch (TooManyListeners e) {
				Application.serialport.close();
				e.printStackTrace();
			}
		}
	}
	
	private class SerialListener implements SerialPortEventListener {
		public void serialEvent(SerialPortEvent serialPortEvent) {
			switch (serialPortEvent.getEventType()) {

			case SerialPortEvent.BI: // 通讯中断
				//ShowUtils.errorMessage("通信中断，请检查设备及连线！");
				break;

			case SerialPortEvent.OE: // 溢出错误

			case SerialPortEvent.FE: // 帧错误

			case SerialPortEvent.PE: // 奇数偶数校验错误

			case SerialPortEvent.CD: // 载波检测
				
			case SerialPortEvent.CTS: // 清除发送

			case SerialPortEvent.DSR: // 数据设备准备好

			case SerialPortEvent.RI: // 振铃指示

			case SerialPortEvent.OUTPUT_BUFFER_EMPTY: // 2 输出缓冲区已清空
				break;

			case SerialPortEvent.DATA_AVAILABLE: // 1串口数据已到达
				byte[] data = null;
				try {
					if (Application.serialport == null) {
						//ShowUtils.errorMessage("串口没有打开");
					} else {
						data = SerialPortManager.readFromPort(Application.serialport);
						if(data!= null && data.length > 0){
							//ConsoleHandler.info(ByteUtils.byteArrayToHexString(data,true));
							Application.processor.PushData(data);// 
						}
					}
				} catch (Exception e) {
					//ShowUtils.errorMessage(e.toString() + "listener");
					// 
				}
				break;
			}
		}
	}
	
	private void closeSerialPort(){
		if(Application.serialport != null){
			Application.serialport.close();
		}
	}
}
